package com.itljm.cxrMarket.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itljm.cxrMarket.common.PageResult;
import com.itljm.cxrMarket.common.Result;
import com.itljm.cxrMarket.entity.Reviews;
import com.itljm.cxrMarket.service.ReviewsService;
import com.itljm.cxrMarket.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ljm
 * @date 2024/12/24 11:06
 */
@RestController
@RequestMapping("/reviews")
@CrossOrigin
public class ReviewsController {
    @Autowired
    private ReviewsService reviewsService;

    // 分页查询所有评论
    @RequestMapping("/info")
    public PageResult<Reviews> selectReviewsPage(Integer pageNum, Integer pageSize, Integer productId,
                                                 String content, Integer merchantId) {
        if (pageNum == null) {
            pageNum = 0;
        }
        if (pageSize == null) {
            pageSize = 15;
        }
        Page<Reviews> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Reviews> wrapper = new QueryWrapper<>();
        wrapper.eq("1",1);

        if (productId != null) {
            wrapper.eq("r.product_id", productId);
        }
        if (content != null) {
            wrapper.like("r.content", content);
        }
        if (merchantId != null && merchantId != 1) {
            wrapper.eq("p.merchant_id", merchantId);
        }

        List<Reviews> reviewsList = reviewsService.selectReviewsPage(page, wrapper);
        return new PageResult<>(reviewsList, page.getTotal());
    }

    // 添加评论
    @RequestMapping("/add")
    public Result addReview(@RequestBody Reviews review) {
        boolean b = reviewsService.addReview(review);
        return b ? Result.ok() : Result.fail();
    }

    // 回复评论
    @RequestMapping("/reply")
    public Result replyReview(@RequestBody Reviews review) {
        if (review.getReplyContent() != null || !review.getReplyContent().equals("")) {
            review.setIsReply(1);
        }
        boolean b = reviewsService.updateReview(review);
        return b ? Result.ok() : Result.fail();
    }

    // 修改评论
    @RequestMapping("/update")
    public Result updateReview(@RequestBody Reviews review) {
        boolean b = reviewsService.updateReview(review);
        return b ? Result.ok() : Result.fail();
    }

    // 根据id查询评论
    @RequestMapping("/info/{id}")
    public Result getReview(@PathVariable("id") Integer reviewId) {
        if (reviewId == null || StringUtils.isEmpty(String.valueOf(reviewId))) {
            return Result.fail("评论id不能为空");
        }
        Reviews reviews = reviewsService.getById(reviewId);
        return reviews != null ? Result.ok(reviews) : Result.fail();
    }

    // 点赞评论
    @RequestMapping("/like")
    public Result likeReview(Long userId, Integer reviewId) {
        boolean b = reviewsService.updateLikeNum(userId, reviewId);
        return b ? Result.ok() : Result.fail();
    }

    // 删除评论
    @RequestMapping("/delete/{id}")
    public Result removeReview(@PathVariable("id") Integer reviewId) {
        boolean b = reviewsService.removeById(reviewId);
        return b ? Result.ok("删除成功") : Result.fail("删除失败") ;
    }

    // 查询商品点赞最多的评论
    @RequestMapping("/likeTop")
    public Result selectLikeTop(Integer productId) {
        Reviews reviews = reviewsService.selectLikeTop(productId);
        return Result.ok(reviews);
    }
}
